package com.changgou.pay.controller;

import com.alibaba.fastjson.JSON;
import com.changgou.pay.service.WeixinPayService;
import com.changgou.util.Result;
import com.changgou.util.StatusCode;
import com.github.wxpay.sdk.WXPayUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

/*****
 * @Author: 黑马训练营
 * @Description: com.changgou.pay.controller
 ****/
@RestController
@RequestMapping(value = "/weixin/pay")
public class WeixinPayController {

    @Value("${mq.pay.exchange.order}")
    private String exchange;

    @Value("${mq.pay.queue.order}")
    private String queue;

    @Value("${mq.pay.routing.key}")
    private String routing;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private WeixinPayService weixinPayService;

    /****
     * 接收微信服务器发送的支付状态数据
     */
    @RequestMapping(value = "/notify/url")
    public String notifyUrl(HttpServletRequest request) throws Exception{
        //获取支付结果
        ServletInputStream is = request.getInputStream();

        //接收存储网络输入流(微信服务器返回的支付状态数据)
        ByteArrayOutputStream os = new ByteArrayOutputStream();

        //缓冲区定义
        byte[] buffer = new byte[1024];
        int len = 0;

        //循环读取输入流，并写入到os中
        while ((len=is.read(buffer))!=-1){
            os.write(buffer,0,len);
        }

        //关闭资源
        os.close();
        is.close();

        //将支付结果转成xml的字符串
        String xmlResult = new String(os.toByteArray(),"utf-8");

        System.out.println(xmlResult);
        //将xmlResult转成Map
        Map<String, String> responseMap = WXPayUtil.xmlToMap(xmlResult);

        /***
         * 将支付结果发送给RabbitMQ
         * 从响应结果中获取attach附加参数 {"exchange":"exchange.order","queue":"queueSeckillOrderStatus"}
         */
        Map<String,String> queueMap = JSON.parseObject(responseMap.get("attach"),Map.class);
        rabbitTemplate.convertAndSend(queueMap.get("exchange"),queueMap.get("queue"), JSON.toJSONString(responseMap));
        //rabbitTemplate.convertAndSend(exchange,routing, JSON.toJSONString(responseMap));

        //返回结果
        Map<String,String> resultMap = new HashMap<String,String>();
        resultMap.put("return_code","SUCCESS");
        resultMap.put("return_msg","OK");
        return WXPayUtil.mapToXml(resultMap);
    }

    /***
     * 根据订单号查询订单支付状态
     */
    @GetMapping(value = "/status/query")
    public Result queryStatus(String outtradeno) throws Exception{
        //调用查询订单支付 状态
        Map map = weixinPayService.queryPayStatus(outtradeno);
        return new Result(true,StatusCode.OK,"支付状态查询成功！",map);
    }

    /***
     * 创建二维码
     * seckillorder(attach)
     */
    @GetMapping(value = "/create/native")
    //public Result createNative(String outtradeno,String totalfee) throws Exception{
    public Result createNative(@RequestParam Map<String,String> dataMap) throws Exception{
        //创建二维码
        //Map<String,String> result = weixinPayService.createNative(outtradeno,totalfee);
        Map<String,String> result = weixinPayService.createNative(dataMap);
        return new Result(true, StatusCode.OK,"创建二维码成功！",result);
    }

}






